package com.future.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.future.domain.Manager;
import com.future.domain.Room;
import com.future.domain.Student;
import com.future.dto.*;

import com.future.service.ManagerService;
import com.future.service.RoomService;
import com.future.service.StudentService;
import com.future.service.UserinfoService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.List;

/**
 * @Auther: 田坑坑
 * @Date: 2022/7/5 01:04
 * @Description: 我题序等你回
 */

@RestController
@RequestMapping("room")
public class RoomController {


    @Autowired
    RoomService  roomService;

    @Autowired
    StudentService  studentService;

    @Autowired
    ManagerService managerService;

    @Autowired
    UserinfoService  userinfoService;




    //根据 学生id 数组  遍历 修改 学生 的 寄宿 信息
    @RequestMapping("addRoomForStudents")
    public MsgDto addRoomForStudents(int roomId, String[] ids){

        //根据 宿舍id 获取 宿舍对象的 所在楼栋id
        int  storeyId   =   this.roomService.getById(roomId).getRoomstoreyid();

        //查看 该宿舍 居住了几个学生
        List<RoomForStudentsDto>  list2   =  this.roomService.findStudent(roomId);
        //第一步： 将 住在该 宿舍 的学生 的 宿舍 id 和 楼栋 id 清空 ，将 居住状态全部 改为 否
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("roomId",roomId);

        Student student1  =  new Student();
        student1.setStoreyid(null);
        student1.setRoomid(null);
        student1.setStudentstatus("否");

            // queryWrapper 只要 符合条件 ，单条也可能成为 批量！！！
            this.studentService.update(student1,queryWrapper);


        //批量修改 学生 的寄宿信息
        for (int i = 0; i <ids.length ; i++) {

            Student  student   = new Student();
            student.setId(Integer.parseInt(ids[i]));
            student.setStoreyid(storeyId);
            student.setRoomid(roomId);
            student.setStudentstatus("是");
            this.studentService.updateById(student);
        }
        //查看 该宿舍 居住了几个学生
        List<RoomForStudentsDto>  list1   =  this.roomService.findStudent(roomId);
        //居住了几个学生
        int  mans   =   list1.size();

       Room  room  =   this.roomService.getById(roomId);

        //计算剩余 床位
        int  roomResidue   =  this.roomService.getById(roomId).getRoomnumber()-mans;

        //更新剩余 床位
        room.setRoomresidue(roomResidue);
        this.roomService.updateById(room);
        return   new MsgDto(true,"否极泰来");



    }



    // 根据id判断 宿舍是男宿舍还是 女宿舍
    @RequestMapping("checkRoomsex")
    public MsgDto checkRoomsex(int roomId){

        String  roomsex   =  this.roomService.getById(roomId).getRoomsex();
        List<RoomForSex>  list   =  this.roomService.findRoomForSex(roomsex);

      return   new MsgDto(false,"时来运转",list);
    }




    //判断 宿舍是否 是禁用
    @RequestMapping("checkRoomForStudents")
    public MsgDto checkRoomForStudents(int roomId){


        //如果 该宿舍 是禁用的，返回 false,提示该宿舍为禁用状态，不能分配。
        Room  room   =  this.roomService.getById(roomId);

        if(room.getRoomstatus().equals("禁用")){
            return  new MsgDto(false,"该宿舍为禁用状态，不能分配");
        }else{
            return  new MsgDto(true,"开始分配");
        }


    }




    //点击修改宿舍信息
    //如果宿舍的 剩余 床位 为  6 ，则随意修改宿舍性别 和 启用状态。
    @RequestMapping("changeRoom")
    public MsgDto changeRoom(int roomId,String  roomName, String roomSex,String roomStatus,int roommanagerid){

        //根据宿舍 id  获取宿舍对象
        Room   room   =  this.roomService.getById(roomId);



        //这是  修改性别或更改为 禁用状态, 有要求
        if(!roomSex.equals(room.getRoomsex())||roomStatus.equals("禁用")){

            //如果 空余 不为 6，不能修改
            if(room.getRoomresidue()!=6){

                room.setRoomname(roomName);
                this.roomService.updateById(room);
                return   new MsgDto(false,"修改名称成功！宿舍不为空，不能修改性别或状态");
            }else{

                //如果 空余 为 6，能修改
                room.setRoomname(roomName);
                room.setRoomsex(roomSex);
                room.setRoommanagerid(roommanagerid);
                room.setRoomstatus(roomStatus);

                this.roomService.updateById(room);

                return   new MsgDto(true,"修改成功");
            }

        }else{

            //这是 修改 宿舍名称 和 开启宿舍 和 管理者，随意修改
            room.setRoomname(roomName);
            room.setRoommanagerid(roommanagerid);
            room.setRoomstatus(roomStatus);
            this.roomService.updateById(room);
            return   new MsgDto(true,"修改成功");
        }

    }



    //查询 宿管 集合
    @RequestMapping("findManagers")
    public MsgDto findManagers(){

        List<UserForManagerDto>  list   =  this.userinfoService.userForManager();

        return  new MsgDto(true,"这是宿管集合",list);
    }


    //根据 宿舍id  查询 宿舍对象
    @RequestMapping("Room")
    public MsgDto findRoom(int roomId){

        Room  room   =  this.roomService.getById(roomId);



        return   new MsgDto(true,"查询成功",room);



    }



    //根据 宿舍 id 查询学生集合
    @RequestMapping("findRoomStudents")
    public MsgDto findRoomStudents(int roomId){

        List<RoomForStudentsDto>  list   =  this.roomService.findStudent(roomId);

        return    new MsgDto(true,"查询成功",list);

    }





        //分页查询宿舍
    @RequestMapping("findRoom")
    public PageInfo<RoomDto> findRoom(RoomDto roomDto,int pageNum,int pageSize){

        //pageNum : 第几页
        // pageSize: 每页几行数据

        PageHelper.startPage(pageNum,pageSize);
        List list    =   this.roomService.findRoomS(roomDto.getRoomSex());
        PageInfo pageInfo=new PageInfo(list);


        return pageInfo;
    }

}
